import React, {Component} from 'react';
import './index.less';
import getTreeRoot from './functions/getTreeRoot';
import getChildTree from './functions/getChildTreeData';
import {base} from 'nc-lightapp-front';

const {NCCheckbox} = base;

// 请求过子树数据的节点id存放在这里
// 每次请求前检查此数组，来判断是否请求过，请求过的不再请求
const hasFetchNode = [];

class LeftContent extends Component {
    constructor(props) {
        super(props);

        this.state = {
            pk: '',
            include_cancle_dept: false
        };

        this.loadTreeData = this.loadTreeData.bind(this);
        this.getTreeRootData = this.getTreeRootData.bind(this);
        this.selectNode = this.selectNode.bind(this);
        this.toggleIncludeDept = this.toggleIncludeDept.bind(this);
        this.handleOrgTreeData = this.handleOrgTreeData.bind(this);
    }

    toggleIncludeDept(value) {
        hasFetchNode.length = 0;
        this.setState({
            include_cancle_dept: value,
            pk: ''
        }, () => {
            this.getTreeRootData(this.props.pk_org, this.props.treeType);
            this.props.onSelect('');
        })
    }

    selectNode(pk) {
        this.props.onSelect(pk);
    }

    loadTreeData(pk, treeNode) {
        if (this.props.treeType === 'psnType' || !treeNode.expanded) return;
        let nodePk = treeNode.node.props.eventKey;
        // 此处是自定义的根节点，所以不请求
        if (nodePk === 'custom_root' || hasFetchNode.indexOf(nodePk) >= 0) {
            return;
        }
        hasFetchNode.push(nodePk);

        const {
            pk_org,
            syncTree,
            treeId,
            treeType
        } = this.props;

        getChildTree({
            include_cancle_dept: this.state.include_cancle_dept,
            pk_org: pk_org,
            tree_node_id: nodePk,
            node_type: 'ORG_NODE',
            treeType: treeType
        })
            .then((res) => {
                if (res.success) {
                    this.handleOrgTreeData(res.data, nodePk);
                    syncTree.openNodeByPk(treeId, pk);
                }
            });
    }

    handleOrgTreeData(newData, nodePk) {
        this.addClassForDisAction(newData);
        const {syncTree, treeId} = this.props;
        let treeData = syncTree.getSyncTreeValue(treeId, nodePk);
        treeData.children = newData;
    }

    addClassForDisAction(tar) {
        tar && tar.length && tar.forEach(v => {
            v.isleaf = false;
            if (v.nodeData.nodeValue.hrcanceled) v.titleStyle = {color: 'lightgrey'};
            v.children && this.addClassForDisAction(v.children);
        })
    }

    componentWillReceiveProps(nextProps) {
        // 此处判断如果当前的refpk和下一个refpk不同，就请求左树根节点数据
        // 当前的treeType和前一个不同也要重新请求
        if (nextProps.pk_org !== this.props.pk_org || nextProps.treeType !== this.props.treeType) {
            hasFetchNode.length = 0;
            this.getTreeRootData(nextProps.pk_org, nextProps.treeType);
        }
        if (nextProps.pk_org && nextProps.treeType !== this.props.treeType) {
            hasFetchNode.length = 0;
            this.getTreeRootData(nextProps.pk_org, nextProps.treeType);
        }
    }

    getTreeRootData(pk_org, treeType) {
        const {
            treeId,
            syncTree
        } = this.props;

        const treeTypeMap = {
            adminOrg: this.props.json['hi6007-000096'], /* 国际化处理： 行政组织*/
            psnType: this.props.json['hi6007-000065']/* 国际化处理： 人员类别*/
        };
        let root = [{
            children: [],
            code: treeTypeMap[treeType],
            iconBox: {
                addIcon: true,
                delIcon: true,
                editIcon: true
            },
            id: null,
            isOrgStruSort: false,
            innercode: null,
            key: 'root',
            name: treeTypeMap[treeType],
            nodeData: {
                nodeValue: {}
            },
            pid: null,
            refname: treeTypeMap[treeType],
            refpk: 'custom_root',
            title: treeTypeMap[treeType]
        }];

        if (!pk_org) {
            syncTree.setSyncTreeData(treeId, root);
            return;
        }

        getTreeRoot({
            pk_org: pk_org,
            node_type: 'ORG_NODE',
            treeType: treeType
        })
            .then((res) => {
                root[0].children = res.data;
                if (treeType === 'adminOrg') {
                    root[0].children.forEach(item => item.isleaf = false);
                    syncTree.setSyncTreeData(treeId, root);
                } else {
                    syncTree.setSyncTreeData(treeId, root);
                }
                syncTree.openNodeByPk(treeId, 'custom_root');
            });
    }

    render() {
        const {
            syncTree,
            treeId,
            treeType
        } = this.props;

        return (
            <div className='left-tree'>
                {syncTree.createSyncTree({
                    treeId: treeId,
                    needSearch: false,
                    needEdit: false,
                    showLine: true,
                    onSelectEve: this.selectNode,
                    onTreeExpand: this.loadTreeData
                })}
                {/*{treeType === 'adminOrg' ?
                    syncTree.createSyncTree({
                        treeId: treeId,
                        needSearch: false,
                        needEdit: false,
                        showLine: true,
                        onSelectEve: this.selectNode,
                        onTreeExpand: this.loadTreeData
                    }) : syncTree.createSyncTree({
                        treeId: treeId2,
                        needSearch: false,
                        needEdit: false,
                        showLine: true,
                        onSelectEve: this.selectNode,
                        defaultExpandAll: true
                    })}*/}
                {treeType === 'adminOrg' ?
                    <div className='left-tree-bottom'>
                        <NCCheckbox
                            checked={this.state.include_cancle_dept}
                            className="tree-checkbox"
                            color="primary"
                            onChange={this.toggleIncludeDept}
                        >
                            {this.props.json['hi6007-000097']}{/* 国际化处理： 包含已撤销部门*/}
                        </NCCheckbox>
                    </div> : null}
            </div>
        );
    }
}

export default LeftContent;
